﻿Imports LatoolNet

Class Example

    Shared Sub Main()

        ComplexExample()
        MatrixInverseExample()
        MatrixFactorizeSolveExample()
        MatrixSolveExample()
        ComplexMatrixInverseExample()
        TridiagonalMatrixExample()
        PolynomialApproximationExample()
        SingularValueDecompositionExample()
        EigenproblemExample()

    End Sub

    Private Shared Sub ComplexExample()


        Dim c1 As Complex = New Complex(1.0, 1.0)

        Dim c2 As Complex = New Complex()
        c2.Real = 2.0
        c2.Imag = 2.0

        Dim c3 As Complex = c1 + c2

        ' e ^ (pi * i) should give -1
        Dim c4 As Complex = Complex.Pow(Math.E, Math.PI * New Complex(0, 1))

        Console.WriteLine(c3.ToString(4))
        Console.WriteLine(c4.ToString(4))

        'Expected output
        '(3.0000,3.0000)
        '(-1.0000,0.0000)

    End Sub

    Private Shared Sub MatrixInverseExample()

        Dim a As Matrix = New Matrix(3, 3)


        a.Item(0, 0) = 1.0
        a.Item(0, 1) = -2.0
        a.Item(0, 2) = 3.0

        a.Item(1, 0) = 3.0
        a.Item(1, 1) = -1.0
        a.Item(1, 2) = 2.0

        a.Item(2, 0) = -1.0
        a.Item(2, 1) = -2.0
        a.Item(2, 2) = 3.0

        Dim ainv As Matrix = a.Clone().Inv()

        Dim unit As Matrix = a * ainv

        Console.WriteLine(unit.ToString(4))

        'Expected output
        '[1.0000,0.0000,0.0000]
        '[0.0000,1.0000,0.0000]
        '[0.0000,0.0000,1.0000]

    End Sub

    Private Shared Sub MatrixFactorizeSolveExample()


        Dim a As Matrix = New Matrix(3, 3)

        a(0, 0) = 2
        a(0, 1) = 3
        a(0, 2) = -1

        a(1, 0) = 4
        a(1, 1) = 4
        a(1, 2) = -3

        a(2, 0) = -2
        a(2, 1) = 3
        a(2, 2) = -1

        Dim b As Matrix = New Matrix(3, 1)

        b(0, 0) = 5
        b(1, 0) = 3
        b(2, 0) = 1

        'LUFactorization.Factorize(a)
        LinearEquation.Factorize(a)

        'LUFactorization.Solve(a, b)
        LinearEquation.Solve(a, b)

        Console.WriteLine(b.ToString(3))

        'Expected output
        '[1.000]
        '[2.000]
        '[3.000]

    End Sub

    Private Shared Sub MatrixSolveExample()

        Dim a As Matrix = New Matrix(3, 3)

        a(0, 0) = 2
        a(0, 1) = 3
        a(0, 2) = -1

        a(1, 0) = 4
        a(1, 1) = 4
        a(1, 2) = -3

        a(2, 0) = -2
        a(2, 1) = 3
        a(2, 2) = -1

        Dim b As Matrix = New Matrix(3, 1)

        b(0, 0) = 5
        b(1, 0) = 3
        b(2, 0) = 1

        'LUFactorization.Solve(a, b)
        LinearEquation.Solve(a, b)

        Console.WriteLine(b.ToString(3))

        'Expected output
        '[1.000]
        '[2.000]
        '[3.000]
    End Sub

    Private Shared Sub ComplexMatrixInverseExample()

        Dim a As ComplexMatrix = New ComplexMatrix(3, 3)

        Dim rgen As Random = New Random()

        a(0, 0) = New Complex(rgen.NextDouble(), rgen.NextDouble())
        a(0, 1) = New Complex(rgen.NextDouble(), rgen.NextDouble())
        a(0, 2) = New Complex(rgen.NextDouble(), rgen.NextDouble())

        a(1, 0) = New Complex(rgen.NextDouble(), rgen.NextDouble())
        a(1, 1) = New Complex(rgen.NextDouble(), rgen.NextDouble())
        a(1, 2) = New Complex(rgen.NextDouble(), rgen.NextDouble())

        a(2, 0) = New Complex(rgen.NextDouble(), rgen.NextDouble())
        a(2, 1) = New Complex(rgen.NextDouble(), rgen.NextDouble())
        a(2, 2) = New Complex(rgen.NextDouble(), rgen.NextDouble())

        Dim ainv As ComplexMatrix = a.Clone().Inv()

        Dim unit As ComplexMatrix = a * ainv

        Console.WriteLine(unit.ToString(3))

        'Expected output
        '[(1.000,0.000),(0.000,0.000),(0.000,0.000)]
        '[(0.000,0.000),(1.000,0.000),(0.000,0.000)]
        '[(0.000,0.000),(0.000,0.000),(1.000,0.000)]

    End Sub


    Private Shared Sub TridiagonalMatrixExample()

        Dim bandwidth As Integer = 3
        Dim tri As Matrix = New Matrix(5, 5, bandwidth)

        tri(0, 0) = 2
        tri(0, 1) = -1

        tri(1, 0) = -1
        tri(1, 1) = 2
        tri(1, 2) = -1

        tri(2, 1) = -1
        tri(2, 2) = 2
        tri(2, 3) = -1

        tri(3, 2) = -1
        tri(3, 3) = 2
        tri(3, 4) = -1

        tri(4, 3) = -1
        tri(4, 4) = 2

        Console.WriteLine(tri.ToString(3))

        'Expected output
        '[2.000,-1.000,0.000,0.000,0.000]
        '[-1.000,2.000,-1.000,0.000,0.000]
        '[0.000,-1.000,2.000,-1.000,0.000]
        '[0.000,0.000,-1.000,2.000,-1.000]
        '[0.000,0.000,0.000,-1.000,2.000]

        Dim expected_x As Matrix = New Matrix(5, 1)
        expected_x(0, 0) = 1
        expected_x(1, 0) = 3
        expected_x(2, 0) = 3
        expected_x(3, 0) = 3
        expected_x(4, 0) = 1

        Dim b As Matrix = tri * expected_x

        'LUFactorization.Solve(tri, b)
        LinearEquation.Solve(tri, b)

        Console.WriteLine(b.ToString(3))

        'Expected output
        '[1.000]
        '[3.000]
        '[3.000]
        '[3.000]
        '[1.000]
    End Sub


    Private Shared Sub PolynomialApproximationExample()

        Dim a As Double = 1.234
        Dim b As Double = 2.354
        Dim c As Double = -4.245
        Dim d As Double = 2.987
        'y = a * x^3 + b * x^2 + c * x + d

        Dim x(4) As Double
        Dim y(4) As Double

        x(0) = -6
        y(0) = a * x(0) * x(0) * x(0) + b * x(0) * x(0) + c * x(0) + d

        x(1) = -3
        y(1) = a * x(1) * x(1) * x(1) + b * x(1) * x(1) + c * x(1) + d

        x(2) = 0
        y(2) = a * x(2) * x(2) * x(2) + b * x(2) * x(2) + c * x(2) + d

        x(3) = 3
        y(3) = a * x(3) * x(3) * x(3) + b * x(3) * x(3) + c * x(3) + d

        x(4) = 6
        y(4) = a * x(4) * x(4) * x(4) + b * x(4) * x(4) + c * x(4) + d

        Dim dimension As Integer = 3
        Dim poly As Polynomial = PolynomialFitting.Fit(x, y, dimension)

        Dim coefs() As Double = poly.Coeffcients

        For i As Integer = coefs.Length - 1 To 0 Step -1
            Console.WriteLine(coefs(i).ToString("#.###"))
        Next



        'Expected output
        '1.234
        '2.354
        '-4.245
        '2.987
    End Sub

    Private Shared Sub SingularValueDecompositionExample()



        Dim mat As Matrix = New Matrix(6, 4)

        mat(0, 0) = 2.27
        mat(0, 1) = -1.54
        mat(0, 2) = 1.15
        mat(0, 3) = -1.94

        mat(1, 0) = 0.28
        mat(1, 1) = -1.67
        mat(1, 2) = 0.94
        mat(1, 3) = -0.78

        mat(2, 0) = -0.48
        mat(2, 1) = -3.09
        mat(2, 2) = 0.99
        mat(2, 3) = -0.21

        mat(3, 0) = 1.07
        mat(3, 1) = 1.22
        mat(3, 2) = 0.79
        mat(3, 3) = 0.63

        mat(4, 0) = -2.35
        mat(4, 1) = 2.93
        mat(4, 2) = -1.45
        mat(4, 3) = 2.30

        mat(5, 0) = 0.62
        mat(5, 1) = -7.39
        mat(5, 2) = 1.03
        mat(5, 3) = -2.57

        Console.WriteLine(mat.ToString)

        Dim sigma As Matrix = Nothing
        Dim U As Matrix = Nothing
        Dim VT As Matrix = Nothing

        SingularValueDecomposition.Decompose(mat, sigma, U, VT)

        Console.WriteLine(sigma.ToString)
        Console.WriteLine(U.ToString)
        Console.WriteLine(VT.ToString)

        'The matrix below should be equal to the original.
        Dim verify As Matrix = U * sigma * VT

        Console.WriteLine(verify.ToString)
    End Sub

    Private Shared Sub EigenproblemExample()

        Dim rownum As Integer = 4
        Dim colnum As Integer = 4

        'Only symmetric type is supported for eigen problem. 
        Dim mat As New Matrix(rownum, colnum, MatrixType.DoubleSymmetric)

        mat(0, 0) = 1.0
        mat(0, 1) = 2.0
        mat(0, 2) = 3.0
        mat(0, 3) = 4.0

        mat(1, 1) = 2.0
        mat(1, 2) = 3.0
        mat(1, 3) = 4.0

        mat(2, 2) = 3.0
        mat(2, 3) = 4.0

        mat(3, 3) = 4.0

        Dim orig As Matrix = mat.Clone()

        Dim resultValues() As Double = Nothing
        Dim resultVectors As Matrix = Nothing

        Eigenproblem.Solve(mat, resultValues, resultVectors)

        For i As Integer = 0 To resultValues.Length - 1
            Dim lambda As Double = resultValues(i)
            Dim vector As Matrix = Matrix.ColVector(resultVectors, i)

            'Since A x = lambda x, lhs and rhs should be same.
            Dim lhs As Matrix = orig * vector
            Dim rhs As Matrix = lambda * vector

            Console.WriteLine(lhs.ToString())
            Console.WriteLine(rhs.ToString())

        Next

    End Sub


End Class
